package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import com.jt.pojo.ItemCatMap;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Component
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public List<ItemCat> testFindAll() {
        return itemCatMapper.selectList(null);
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {

        itemCatMapper.updateById(itemCat);
    }

    @Override
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        QueryWrapper<ItemCat> wrapper = new QueryWrapper<>();
        if (itemCat.getLevel() == 3){
            itemCatMapper.deleteById(itemCat.getId());
        }
        if(itemCat.getLevel() == 2){
            wrapper.clear();
//            wrapper.eq("parent_id", itemCat.getId());
//            List<ItemCat> itemCatList = itemCatMapper.selectList(wrapper);
//            for(ItemCat three:itemCatList){
//                itemCatMapper.deleteById(three.getId());
//            }
//            itemCatMapper.deleteById(itemCat.getId());
            wrapper.eq("parent_id", itemCat.getId())
                    .or()
                    .eq("id", itemCat.getId());
            itemCatMapper.delete(wrapper );
        }
        if (itemCat.getLevel() == 1){
            wrapper.clear();
            wrapper.eq("parent_id", itemCat.getId());
            List twoIds = itemCatMapper.selectObjs(wrapper);
            wrapper.clear();
            boolean flag = twoIds == null || twoIds.size() == 0?false:true;
            wrapper.in(flag, "parent_id", twoIds)
                    .or()
                    .eq("parent_id", itemCat.getId())
                    .or()
                    .eq("id", itemCat.getId());
            itemCatMapper.delete(wrapper);

        }
    }
    /**以下为三级菜单列表的嵌套结构完成示例 使用map集合辅助完成*/
        @Override
        public List<ItemCat> findItemCatList(Integer level) {
//        long t1 = System.currentTimeMillis();
//        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", 0);
//        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
//        for (ItemCat one:oneList){
//            queryWrapper.clear();
//            queryWrapper.eq("parent_id", one.getId());
//            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
//            for (ItemCat two:twoList){
//                queryWrapper.clear();
//                queryWrapper.eq("parent_id", two.getId());
//                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
//                two.setChildren(threeList);
//            }
//            one.setChildren(twoList);
//            long t2 = System.currentTimeMillis()-t1;
//            System.out.println(t2);
//        }
    /**数据结构：map<k,v>  key=parentid  value=list<ItemCat>
     *
     * */
        long t1 = System.currentTimeMillis();
        Map<Integer, List<ItemCat>> map = getMap();
        if (level==1){
            return map.get(0);
        }
        if(level==2) {
            return getTwoList(map);
        }
        long t2 = System.currentTimeMillis()-t1;
        System.out.println(t2);
        return getThreeList(map);
    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
//        List<ItemCat> oneList = map.get(0);
        List<ItemCat> oneList = getTwoList(map);
        for(ItemCat oneItem:oneList){
//            List<ItemCat> twoList = map.get(oneItem.getId());
            List<ItemCat> twoList = oneItem.getChildren();
            if(twoList == null || twoList.size() == 0){continue;}
            for(ItemCat twoItem:twoList){
                List<ItemCat> threeList = map.get(twoItem.getId());
                twoItem.setChildren(threeList);
            }
        }
        return oneList;
    }
    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for(ItemCat oneItem:oneList){
            List<ItemCat> twoList = map.get(oneItem.getId());
            oneItem.setChildren(twoList);
        }
        return oneList;
    }

    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map=new HashMap<>();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
      for(ItemCat itemCat:itemCatList){
          if(map.containsKey(itemCat.getParentId())){
              map.get(itemCat.getParentId()).add(itemCat);
          }else {
              List<ItemCat> childrenList = new ArrayList<>();
              childrenList.add(itemCat);
              map.put(itemCat.getParentId(), childrenList);
          }
      }
        return map;
    }
}
